<?php
namespace app\admin\controller;

class Category extends Base{

    protected $model;
    //protected $typeModel;
    public function __construct(){
        parent::__construct();
        $this->model = model('Category');
        //$this->typeModel = model('Type');
    }

    public function lst(){
        $data = $this->model->getTree();
        $this->assign("data", $data);
        return $this->fetch();
    }

    public function add(){
        if(request()->isPost()){
            $data = request()->param();
            if($this->model->saveData($data,false)){
                return json($this->success('保存成功！'));
            }else{
                return json($this->error('保存失败！'));
            }
        }
        $parentData = $this->model->getTree();
        $this->assign('parentData', $parentData);
        // 取出所有的商品分类制作下拉框
        //$typeData = $this->typeModel->select();
        //$this->assign('typeData', $typeData);
        return $this->fetch();
    }
    public function edit(){
        $id = input('id');
        if(request()->isPost()){
            $data = request()->param();
            if($this->model->saveData($data,true)){
                return json($this->success('保存成功！'));
            }else{
                return json($this->error('保存失败！'));
            }
        }

        $data = $this->model->queryDataByID($id);
        $this->assign('data',$data);
        return $this->fetch();
    }
}

